package com.sicau.socialpracticesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.socialpracticesystem.entity.param.user.UserQueryParam;
import com.sicau.socialpracticesystem.entity.po.User;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.user.UserVO;
import com.sicau.socialpracticesystem.mapper.UserMapper;
import com.sicau.socialpracticesystem.mapper.VOMapper;
import com.sicau.socialpracticesystem.service.UserService;
import com.sicau.socialpracticesystem.util.PageUtil;
import com.sicau.socialpracticesystem.util.QueryWrapperUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 排序属性
     */
    private static final String[] ORDER_COLUMN = {"id"};

    @Resource
    UserMapper userMapper;

    @Resource
    private VOMapper.UserVOMapper userVOMapper;

    @Override
    public PageDataVO<UserVO> queryUsersByCondition(UserQueryParam userQueryParam) {
        QueryWrapper<UserVO> queryWrapper = QueryWrapperUtil.getQueryWrapper(UserVO.class, UserQueryParam.class, userQueryParam);
        queryWrapper.orderByDesc(ORDER_COLUMN);
        Page<UserVO> infoVOPage = new Page<>(userQueryParam.getCurrentPage(), userQueryParam.getPageSize());
        infoVOPage = userVOMapper.selectPage(infoVOPage, queryWrapper);
        return PageUtil.convert(infoVOPage);
    }

    @Override
    public UserVO queryById(Integer id) {
        return userVOMapper.selectById(id);
    }

    @Override
    public User queryUserByNumber(Integer number) {
        if (number == null) {
            return null;
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getNumber, number);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserVO queryUsersById(Integer id) {
        return userVOMapper.selectById(id);
    }

}
